#I Import and Initialize
import  sys, os
import pygame
import random
from data import *
from sprites import *
from scenes import *

class Camera(object):
    def __init__(self, player, rect):
        self.player = player
        self.rect   = rect
        
    def update(self):
        if self.player.rect.right >= self.rect.width and self.player.x_velocity > 0:
            self.player.x_velocity = 0
        if self.player.rect.left <= self.rect.left and self.player.x_velocity < 0:
            self.player.x_velocity = 0
        if self.player.rect.top <= self.rect.top and self.player.y_velocity < 0:
            self.player.y_velocity = 0
        if self.player.rect.bottom >= self.rect.height and self.player.y_velocity > 0:
            self.player.y_velocity = 0
            
        self.rect.clamp_ip(self.rect)   
    
    def draw(self, surf, playerSprite):
        subsurface = surf.subsurface(self.rect)
        playerSprite.draw(subsurface)

class Game(object):
    
    def __init__(self, screen):
              
        self.SCREEN_WIDTH = 800
        self.SCREEN_HEIGHT = 600
        
        self.screen      = screen
        
        self.background1 = loadImage("background.png") #zet background
        self.background2 = loadImage("background.png")
        self.birdUps     = loadImage("bird-ups.png")
        
        self.bgrect1     = self.background1.get_rect() #retourneert BG Rect
        self.bgrect2     = self.background2.get_rect()
        
        self.bgrect1     = self.bgrect1.move(0, 0)
        self.bgrect2     = self.bgrect2.move(self.SCREEN_WIDTH, 0)
        
        #A Action onderverdeeld in ALTER
        
        #A Assign
        self.playerSprite     = pygame.sprite.RenderClear()
        self.ballSprite       = pygame.sprite.RenderClear()
        self.cannonSprite     = pygame.sprite.RenderClear()
        self.explosionSprite  = pygame.sprite.RenderClear()
        self.cannonBallSprite = pygame.sprite.RenderClear()
        
        self.player          = Player(10, 60)
        self.cannon          = Cannon(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
        self.camera          = Camera(self.player, self.screen.get_rect())
        self.ball            = pygame.sprite.Sprite
        
        self.playerSprite.add(self.player)
        self.cannonSprite.add(self.cannon)
        
        self.score       = 0
        self.lives       = 3
        self.time        = 400
        self.lvl         = 1
        self.font        = pygame.font.Font(filepath("fonts/font1.ttf"), 16)
        self.speed       = [4, 4]
        self.bgspeed     = [-1,0]
        self.white       = 255, 255, 255
        self.clock       = pygame.time.Clock()
        self.keepGoing   = True
        self.frame       = 25
        
        self.main_loop()
              
    def gameover_screen(self):
        cutscene(self.screen, ["Game Over"])
        self.keepGoing = False 
        
    def delay(self):
        while self.frame <= 150: 
            self.frame += 1
            return False
        else:
            self.frame = 0
            return True
        
    def draw_stats(self):
        firstLine = 16
        secondLine = 35
        
        lives = self.lives
        if lives < 0:
            lives = 0
        
        ren = self.font.render("Score: %05d" % self.score, 1, (255, 255, 255))
        self.screen.blit(ren, (608, firstLine))
               
        ren = self.font.render("Level: %0d" % self.lvl, 1, (255, 255, 255))
        self.screen.blit(ren, (608, secondLine))
        
        ren = self.font.render("x%d" % lives, 1, (255, 255, 255))
        self.screen.blit(ren, (478, firstLine))
               
        ren = self.font.render("FPS: %d" % self.clock.get_fps(), 1, (255, 255, 255))
        self.screen.blit(ren, (478, secondLine))
        
        self.screen.blit(self.birdUps, (420, firstLine))
    
    def space_pressed(self):
        self.ball = PlayerDef((self.player.rect.centerx - 30), self.player.rect.centery)
        self.ballSprite.add(self.ball)
    
    def go_horizontal(self, x):
        self.player.x_velocity = x
    
    def go_vertical(self, y):
        self.player.y_velocity = y

        #L Loop
    def main_loop(self):    
        while self.keepGoing:
            #T Timer (framerate)
            self.clock.tick(90)
                    
            #E Event
            self.bgrect1 = self.bgrect1.move(self.bgspeed) # bewegende achtergrond
            self.bgrect2 = self.bgrect2.move(self.bgspeed)
            if self.bgrect1.right < 0:
                self.bgrect1 = self.bgrect1.move((self.SCREEN_WIDTH*2, 0))
            if self.bgrect2.right < 0:
                self.bgrect2 = self.bgrect2.move((self.SCREEN_WIDTH*2, 0))
            
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.keepGoing = False
                    elif event.key == pygame.K_LEFT:
                        self.go_horizontal(-4)
                    elif event.key == pygame.K_RIGHT:
                        self.go_horizontal(4)
                    elif event.key == pygame.K_UP:
                        self.go_vertical(-4)
                    elif event.key == pygame.K_DOWN:
                        self.go_vertical(4)
                    elif event.key == pygame.K_SPACE:
                        self.space_pressed()
                                
                elif event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT:
                        self.go_horizontal(0)
                    elif event.key == pygame.K_RIGHT:
                        self.go_horizontal(0)
                    elif event.key == pygame.K_UP:
                        self.go_vertical(0)
                    elif event.key == pygame.K_DOWN:
                        self.go_vertical(0)
                
            if pygame.sprite.groupcollide(self.ballSprite, self.cannonSprite, 1, 1):
                self.explosionSprite.add(Explosion(self.cannon.rect.center))
                self.cannon = Cannon(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
                self.cannonSprite.add(self.cannon)
                self.score += 10
            
            for hit in pygame.sprite.groupcollide(self.cannonBallSprite, self.playerSprite, 1, 0):
                self.score -= 50
               
            for hit in pygame.sprite.groupcollide(self.cannonSprite, self.playerSprite, 0, 1):
                self.score = -1   
                
            if self.delay() == True:
                self.cannonBallSprite.add(CannonDef(self.cannon.rect.centerx, self.cannon.rect.centery))
                
            if self.score < 0:
                self.gameover_screen()
                
            #R refresh
            self.camera.update()
            self.playerSprite.update()
            self.ballSprite.update(self.SCREEN_HEIGHT)
            self.cannonSprite.update(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
            self.explosionSprite.update()
            self.cannonBallSprite.update(self.SCREEN_WIDTH, self.player.rect.center, self.cannon.rect.center)
            
            self.screen.fill(self.white)
            self.screen.blit(self.background1, self.bgrect1)
            self.screen.blit(self.background2, self.bgrect2)
            
            self.draw_stats()
            
            self.camera.draw(self.screen, self.playerSprite)
            
            self.ballSprite.draw(self.screen)
            self.explosionSprite.draw(self.screen)
            self.cannonBallSprite.draw(self.screen)
            self.cannonSprite.draw(self.screen)
            
            pygame.display.flip()
